package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.service.TtransferinfoService;



@Controller
public class TtransferinfoAction{
	private static final Logger log = LoggerFactory.getLogger(TtransferinfoAction.class);
	public TtransferinfoAction() {
	}
 
 
 	/**
	 * 转让标的信息表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferinfoAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("ttransferinfo", new Ttransferinfo());
        setDict(model);
		return "ttransferinfoEdit";
	}

	/**
	 * 转让标的信息表新增保存
	 * 
	 * @param ttransferinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_ttransferinfoSave.do")
	public String save(Ttransferinfo ttransferinfo, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(ttransferinfo, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ttransferinfo = ttransferinfoService.save(ttransferinfo);
		return "redirect:v_ttransferinfoList.do";
	}

	/**
	 * 转让标的信息表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_ttransferinfoDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				ttransferinfoService.remove(id);
			}
		}
		return "redirect:v_ttransferinfoList.do";
	}

	/**
	 * 转让标的信息表编辑页面
	 * 
	 * @param tiid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferinfoEdit.do")
	public String edit(long tiid, HttpServletRequest request, ModelMap model) {
		Ttransferinfo ttransferinfo = ttransferinfoService.get(tiid);
		model.addAttribute("ttransferinfo", ttransferinfo);
        setDict(model);
		return "ttransferinfoEdit";
	}

	/**
	 * 转让标的信息表详情
	 * 
	 * @param tiid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferinfoDetail.do")
	public String detail(long tiid, HttpServletRequest request, ModelMap model) {
		Ttransferinfo ttransferinfo = ttransferinfoService.get(tiid);
		model.addAttribute("ttransferinfo", ttransferinfo);
        setDict(model);
		return "ttransferinfoView";
	}

	/**
	 * 转让标的信息表查询（分页）
	 * 
	 * @param ttransferinfo
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferinfoList.do")
	public String listPage(Ttransferinfo ttransferinfo, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = ttransferinfoService.getTtransferinfoByPage(toMap(ttransferinfo),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "ttransferinfoList";
	}

	/**
	 * 转让标的信息表查询
	 * 
	 * @param ttransferinfo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferinfoList1.do")
	public String list(Ttransferinfo ttransferinfo, HttpServletRequest request, ModelMap model) {
		List<Ttransferinfo> list = ttransferinfoService.getTtransferinfo(toMap(ttransferinfo));
		model.addAttribute("list", list);
        setDict(model);
		return "ttransferinfoList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttransferinfo
	 * @return
	 */	private Map<String,Object> toMap(Ttransferinfo ttransferinfo){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttransferinfo.getTiid()!=null&&!ttransferinfo.getTiid().equals("")){
			map.put("tiid", ttransferinfo.getTiid());
		}
		if(ttransferinfo.getTtenderrecord()!=null&&!ttransferinfo.getTtenderrecord().equals("")){
			map.put("Ttenderrecord", ttransferinfo.getTtenderrecord());
		}
		if(ttransferinfo.getTmember()!=null&&!ttransferinfo.getTmember().equals("")){
			map.put("Tmember", ttransferinfo.getTmember());
		}
		if(ttransferinfo.getTrmoney()!=null&&!ttransferinfo.getTrmoney().equals("")){
			map.put("trmoney", ttransferinfo.getTrmoney());
		}
		if(ttransferinfo.getTrfactor()!=null&&!ttransferinfo.getTrfactor().equals("")){
			map.put("trfactor", ttransferinfo.getTrfactor());
		}
		if(ttransferinfo.getTrreason()!=null&&!ttransferinfo.getTrreason().equals("")){
			map.put("trreason", ttransferinfo.getTrreason());
		}
		if(ttransferinfo.getTrprice()!=null&&!ttransferinfo.getTrprice().equals("")){
			map.put("trprice", ttransferinfo.getTrprice());
		}
		if(ttransferinfo.getStatus()!=null&&!ttransferinfo.getStatus().equals("")){
			map.put("status", ttransferinfo.getStatus());
		}
		if(ttransferinfo.getIstransfer()!=null&&!ttransferinfo.getIstransfer().equals("")){
			map.put("istransfer", ttransferinfo.getIstransfer());
		}
		if(ttransferinfo.getTsfmoney()!=null&&!ttransferinfo.getTsfmoney().equals("")){
			map.put("tsfmoney", ttransferinfo.getTsfmoney());
		}
		if(ttransferinfo.getTrdatetime()!=null&&!ttransferinfo.getTrdatetime().equals("")){
			map.put("trdatetime", ttransferinfo.getTrdatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TTRANSFERINFO_STATUS));
      model.addAttribute("istransferMap", DictUtils.getDictByType(DictUtils.TTRANSFERINFO_ISTRANSFER));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttransferinfo ttransferinfo, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TtransferinfoService ttransferinfoService;   
}